package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.domain.SysConfig;
import com.itheima.youdianda.dto.SysConfigDto;
import com.itheima.youdianda.service.SysConfigService;
import com.itheima.youdianda.mapper.SysConfigMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author YUAN
* @description 针对表【sys_config】的数据库操作Service实现
* @createDate 2022-09-06 18:36:38
*/
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig>
    implements SysConfigService{
    @Autowired
    private SysConfigMapper configMapper;
    @Override
    public Map<String, Object> all() {
        Map<String, Object> map = new HashMap<>();
        List<SysConfig> configs = configMapper.selectList(new LambdaQueryWrapper<SysConfig>()
                .orderByAsc(SysConfig::getSortNum));

        List<SysConfig> allData = new ArrayList<>();
        Map<String, String> configForm = new HashMap<>();
        Map<String, String> configRules = new HashMap<>();

        List<SysConfigDto> dtoList = configs.stream().map((config) -> {
            SysConfigDto dto = new SysConfigDto();
            BeanUtils.copyProperties(config,dto);

            //处理 allData
            doConfigValue(dto,config);

            // 处理 configForm
            configForm.put(config.getAttrKey(),config.getAttrValue());

            // 处理configRules
            Map<String, String> rules = doConfigRules(config);
            BeanUtils.copyProperties(rules,configRules);

            return dto;
        }).collect(Collectors.toList());


        map.put("allData", dtoList);
        map.put("configForm", configForm);
        map.put("configRules", configRules);

        List<HashMap<String, Object>> list = super.list(new LambdaQueryWrapper<SysConfig>().select(SysConfig::getTabValue).groupBy(SysConfig::getTabValue)).stream().map(config -> {
            HashMap<String, Object> configMap = new HashMap<>();
            switch (config.getTabValue()) {
                case "base":
                    configMap.put("key", "base");
                    configMap.put("name", "基础配置");
                    break;
                case "system":
                    configMap.put("key", "system");
                    configMap.put("name", "系统设置");
                    break;
                case "custom":
                    configMap.put("key", "custom");
                    configMap.put("name", "其他设置");
                    break;
                default:
                    break;
            }
            configMap.put("children",
                    super.list(new LambdaQueryWrapper<SysConfig>().select(SysConfig::getGroupValue)
                            .eq(SysConfig::getTabValue, config.getTabValue()).groupBy(SysConfig::getGroupValue)).stream().map(conf -> {
                        HashMap<String, Object> confMap = new HashMap<>();
                        confMap.put("name", conf.getGroupValue());

                        List<SysConfig> configList = super.list(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getTabValue, config.getTabValue())
                                .eq(SysConfig::getGroupValue, conf.getGroupValue()));
                        List<SysConfigDto> configDtoList = configList.stream().map(item -> {
                            SysConfigDto configDto = new SysConfigDto();
                            BeanUtils.copyProperties(item, configDto);
                            //处理 allData
                            doConfigValue(configDto, item);
                            return configDto;
                        }).collect(Collectors.toList());
                        confMap.put("data", configDtoList);
                        return confMap;
                    }).collect(Collectors.toList()));
            return configMap;
        }).collect(Collectors.toList());
        map.put("list", list);
        return map;
    }

    /**
     * 处理configRules
     * @param config
     * @return
     */
    private Map<String, String> doConfigRules(SysConfig config) {
        Map<String, String> ruleHashMap = new HashMap<>();
        Integer type = config.getType();
        if(type == 4) {
            String[] rules = config.getConfigValue().split("&");
            for (String rule : rules) {
                String[] split = rule.split("=");
                ruleHashMap.put("label", split[0]);
                ruleHashMap.put("value", split[1]);
            }
        }
        return ruleHashMap;
    }



    /**
     * 处理 ConfigValue
     *
     *
     * @param dto
     * @param config
     * @return
     */
    public SysConfigDto doConfigValue(SysConfigDto dto, SysConfig config) {

        String config_value = config.getConfigValue();
        if (config_value != null && !"".equals(config_value)) {
            Integer type = config.getType();
            if (type == 4) {
                String[] rules = config_value.split("&");
                List<Map<String, Object>> configValue = new ArrayList<>();
                for (String rule : rules) {
                    String[] split = rule.split("=");
                    Map<String, Object> ruleHashMap = new HashMap<>();

                    ruleHashMap.put("label", split[0]);
                    ruleHashMap.put("value", split[1]);
                    configValue.add(ruleHashMap);
                }
                dto.setConfigValueList(configValue);

                return dto;
            }
        }

        return null;
    }
}




